package com.pengcheng.taskapplication.net;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.pengcheng.taskapplication.TaskApplication;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.BaseEntity;
import com.pengcheng.taskapplication.mvp.view.activity.LoginActivity;
import com.pengcheng.taskapplication.mvp.view.activity.LoginActivityNew;
import com.pengcheng.taskapplication.mvp.view.activity.MainActivity;
import com.pengcheng.taskapplication.util.SPUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;

import okhttp3.MediaType;
import okhttp3.ResponseBody;
import retrofit2.Converter;

import static okhttp3.internal.Util.UTF_8;

//CustomGsonResponseBodyConverter.java
final class CustomGsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final TypeAdapter<T> adapter;

    CustomGsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        String response = value.string();
        Log.i("res",response);
        BaseEntity httpStatus = gson.fromJson(response, BaseEntity.class);
        if (
                //httpStatus.msg.equals(ConstantValue.TOKENMSG)||
                        httpStatus.resultCode.equals("903")
        ||httpStatus.resultCode.equals("904")||httpStatus.resultCode.equals("905")||httpStatus.resultCode.equals("906")) {
            value.close();
            if(MainActivity.mActivity!=null) {
                MainActivity.mActivity.finish();
            }
            TaskApplication.getInstance().finishAllActivity();

            Context context= TaskApplication.getInstance().getApplicationContext();
            SPUtils.remove(context, ConstantValue.SpType.token);
            SPUtils.remove(context,ConstantValue.SpType.newtoken);
            SPUtils.remove(context,ConstantValue.SpType.isreal);
            SPUtils.remove(context,ConstantValue.SpType.userid);
            SPUtils.remove(context,ConstantValue.SpType.phone);
            Intent intent = new Intent(context, LoginActivityNew.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(intent);
        }

        MediaType contentType = value.contentType();
        Charset charset = contentType != null ? contentType.charset(UTF_8) : UTF_8;
        InputStream inputStream = new ByteArrayInputStream(response.getBytes());
        Reader reader = new InputStreamReader(inputStream, charset);
        JsonReader jsonReader = gson.newJsonReader(reader);

        try {
            return adapter.read(jsonReader);
        } finally {
            value.close();
        }
    }
}